﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows;

namespace Dotway.WPF.Controls.Utilities
{
    internal class ColorMapData
    {
        public ColorMapData()
        { }

        public Image GetColorMap(Color hColor, int width, int height)
        {
            PixelFormat pixelFormat = PixelFormats.Rgb24;            
            int rawStride = (width * pixelFormat.BitsPerPixel) / 8;
            byte[] rawImage = new byte[rawStride * height];

            int x = -1;
            int y = -1;
            Color startColor = Color.FromRgb(0, 0, 0);
            Color stopColor = Color.FromRgb(0, 0, 0);

            for (int i = 0; i < rawImage.Length; i += 3)
            {
                if (i % rawStride == 0)
                {
                    y++;
                    x = -1;

                    // Interpolate in y;
                    double relativeY = (double)y / height;
                    startColor.R = (byte)(255 + relativeY * (-255));
                    startColor.G = (byte)(255 + relativeY * (-255));
                    startColor.B = (byte)(255 + relativeY * (-255));

                    stopColor.R = (byte)(hColor.R + relativeY * (-hColor.R));
                    stopColor.G = (byte)(hColor.G + relativeY * (-hColor.G));
                    stopColor.B = (byte)(hColor.B + relativeY * (-hColor.B));
                }

                x++;

                // Interpolate in x;
                double relativeX = (double)x / width;
                rawImage[i] = (byte)(startColor.R + relativeX * (stopColor.R - startColor.R));
                rawImage[i + 1] = (byte)(startColor.G + relativeX * (stopColor.G - startColor.G));
                rawImage[i + 2] = (byte)(startColor.B + relativeX * (stopColor.B - startColor.B));
            }

            // Create a BitmapSource.
            BitmapSource bitmap = BitmapSource.Create(width, height, 96, 96, pixelFormat, null, rawImage, rawStride);

            // Set image source.
            Image image = new Image();
            image.Stretch = Stretch.Fill;
            image.Source = bitmap;
            return image;
        }

        public Color GetColor(Color hColor, Point position, int width, int height)
        {
            Color startColor = Color.FromRgb(0, 0, 0);
            Color stopColor = Color.FromRgb(0, 0, 0);
            Color color = Color.FromRgb(0, 0, 0);

            double relativeY = position.Y / height;
            startColor.R = (byte)(255.0 + relativeY * (-255.0));
            startColor.G = (byte)(255.0 + relativeY * (-255.0));
            startColor.B = (byte)(255.0 + relativeY * (-255.0));

            stopColor.R = (byte)(hColor.R + relativeY * (-hColor.R));
            stopColor.G = (byte)(hColor.G + relativeY * (-hColor.G));
            stopColor.B = (byte)(hColor.B + relativeY * (-hColor.B));

            double relativeX = position.X / width;
            color.R = (byte)(startColor.R + relativeX * (stopColor.R - startColor.R));
            color.G = (byte)(startColor.G + relativeX * (stopColor.G - startColor.G));
            color.B = (byte)(startColor.B + relativeX * (stopColor.B - startColor.B));            

            return color;
        }

        public Point GetPosition(out Color hColor, int width, int height, Color selectedColor)
        {
            Point position = new Point();

            HSV hsv = Rgb2HsvConverter.Rgb2Hsv(selectedColor);
            hColor = Rgb2HsvConverter.H2Rgb(hsv.H);
            position.X = hsv.S * width;            
            position.Y = height - hsv.V * height;

            return position;
        }
    }
}
